package net.maku.propagate.controller;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.propagate.convert.PropagateInnovationStudioConvert;
import net.maku.propagate.entity.PropagateInnovationStudioEntity;
import net.maku.propagate.service.PropagateInnovationStudioService;
import net.maku.propagate.query.PropagateInnovationStudioQuery;
import net.maku.propagate.vo.PropagateInnovationStudioVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 创新工作室
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-03-16
*/
@RestController
@RequestMapping("propagate/innovation")
@Tag(name="创新工作室")
@AllArgsConstructor
public class PropagateInnovationStudioController {
    private final PropagateInnovationStudioService propagateInnovationStudioService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('propagate:innovation:page')")
    public Result<PageResult<PropagateInnovationStudioVO>> page(@ParameterObject @Valid PropagateInnovationStudioQuery query){
        PageResult<PropagateInnovationStudioVO> page = propagateInnovationStudioService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('propagate:innovation:info')")
    public Result<PropagateInnovationStudioVO> get(@PathVariable("id") Long id){
        PropagateInnovationStudioEntity entity = propagateInnovationStudioService.getById(id);

        return Result.ok(BeanUtil.copyProperties(entity, PropagateInnovationStudioVO.class));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('propagate:innovation:save')")
    public Result<String> save(@RequestBody PropagateInnovationStudioVO vo){
        propagateInnovationStudioService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('propagate:innovation:update')")
    public Result<String> update(@RequestBody @Valid PropagateInnovationStudioVO vo){
        propagateInnovationStudioService.update(vo);

        return Result.ok();
    }

    @GetMapping("unmount/{id}/{status}")
    @Operation(summary = "上下架")
    @PreAuthorize("hasAuthority('propagate:innovation:unmount')")
    public Result<String> unmount(@PathVariable("id") Long id, @PathVariable("status") Integer status){
        propagateInnovationStudioService.unmount(id,status);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('propagate:innovation:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        propagateInnovationStudioService.delete(idList);

        return Result.ok();
    }
}